package cn.gorit.content.service.impl;

import cn.gorit.common.enums.ResultEnums;
import cn.gorit.common.enums.StateEnums;
import cn.gorit.common.exception.BlogException;
import cn.gorit.common.lang.Result;
import cn.gorit.content.service.BlogService;
import cn.gorit.dto.content.BlogDto;
import cn.gorit.dto.content.BlogReadDto;
import cn.gorit.entity.content.Blog;
import cn.gorit.mapper.content.BlogMapper;
import cn.gorit.utils.IdWorker;
import cn.gorit.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version v1.0
 * @Classname BlogServiceImpl
 * @Description TODO
 * @Author CodingGorit
 * @Created on 2021/4/3 16:42
 * @IDLE IntelliJ IDEA
 **/
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper mapper;

    @Autowired
    private IdWorker idWorker;

    // 添加博客
    @Override
    public Result<Object> save(Blog dto) {
        try {
            dto.setBlogId(idWorker.nextId() + 1 + "");
            mapper.save(dto);
        } catch (Exception e) {
            throw new BlogException("添加出错" + e.getMessage());
        }
        return new Result<>(ResultEnums.ADD_SUCCESS.getCode(),ResultEnums.ADD_SUCCESS.getMsg());
    }

    // 获取博客具体信息
    @Override
    public Result<Blog> getBlogById(String blogId) {
        return new Result<>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),mapper.getBlogById(blogId));
    }

    // 阅读博客
    @Override
    public Result<BlogReadDto> readBlog(String blogId) {
        // 阅读量 + 1
        mapper.updateRead(blogId);
        BlogReadDto dto = mapper.readById(blogId);
        if (dto != null)
            return new Result<>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),dto);
        return new Result<>(ResultEnums.DATA_NOT_FOUND.getCode(),"糟糕，文章已被删除");
    }

    // 根据 dto 修改博客内容，修改博客
    @Override
    public Result<Object> updateBlogByDto(Blog dto) {
        mapper.updateBlog(dto);
        return new Result<>(ResultEnums.UPDATE_SUCCESS.getCode(),ResultEnums.UPDATE_SUCCESS.getMsg());
    }

    // 分页阅读博客 【前台查看自己需要带上查询参数】
    @Override
    public Result<Page<BlogDto>> getBlogListByPage(Page<BlogDto> page) {
        // 查询数据
        List<BlogDto> blogDtoList = mapper.getBlogListByPage(page);
        page.setList(blogDtoList);
        // 查询总数
        int totalCount = mapper.getBlogCountByPage(page);
        page.setTotalCount(totalCount);
        return new Result<>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),page);
    }

    @Override
    public Result<Object> deleteBlogByBlogId(String blogid) {
        mapper.deleteBlog(blogid);
        return new Result<>(ResultEnums.DELETE_SUCCESS.getCode(),ResultEnums.DELETE_SUCCESS.getMsg());
    }

    // 修改top
    @Override
    public Result<Object> updateBlogTopById(Integer status, String blogId) {
        String msg = "";
        if (status.equals(1)) {
            mapper.updateBlogTopById(StateEnums.ENABLE_TOP.getCode(),blogId);
            msg = StateEnums.ENABLE_TOP.getMsg();
        }
        if (status.equals(0)) {
            mapper.updateBlogTopById(StateEnums.DISABLE_TOP.getCode(), blogId);
            msg = StateEnums.DISABLE_TOP.getMsg();
        }
        return new Result<>(ResultEnums.UPDATE_SUCCESS.getCode(),msg);
    }

    // 博主自行开启评论
    @Override
    public Result<Object> updateCommentAble(Integer enable, String blogId) {
        String msg = "";
        if (enable.equals(1)) {
            mapper.updateBlogTopById(StateEnums.ENABLED.getCode(),blogId);
            msg = "已经开启评论";
        }
        if (enable.equals(0)) {
            mapper.updateBlogTopById(StateEnums.DISABLE.getCode(), blogId);
            msg = "已经关闭评论";
        }
        return new Result<>(ResultEnums.UPDATE_SUCCESS.getCode(),msg);
    }

    @Override
    public Result<Object> updateBlogCollection(BlogDto dto) {
        mapper.updateCollection(dto);
        return new Result<>(ResultEnums.UPDATE_SUCCESS.getCode(),ResultEnums.UPDATE_SUCCESS.getMsg());
    }



}
